#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QCursor>
#include <QIcon>
#include <QPixmap>
#include <QDebug>
#include "Controls/popup.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    ,m_page(AllChat)
{
    ui->setupUi(this);
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);

    this->setMouseTracking(true);
    this->installEventFilter(this);

    connect(ui->stickBtn, &QPushButton::clicked, this, &MainWindow::stickEvent);
    connect(ui->mininBtn, &QPushButton::clicked, this, &MainWindow::mininEvent);
    connect(ui->maxinBtn, &QPushButton::clicked, this, &MainWindow::maxinEvent);
    connect(ui->closeBtn, &QPushButton::clicked, this, &MainWindow::clozeEvent);

    connect(ui->allchatBtn, &QPushButton::clicked, this, &MainWindow::selectEvent);
    connect(ui->contactBtn, &QPushButton::clicked, this, &MainWindow::selectEvent);
    connect(ui->collectBtn, &QPushButton::clicked, this, &MainWindow::selectEvent);

    m_btn = ui->allchatBtn;
    ui->gridLayout_2->addWidget(&m_allchatlist);
    m_allchatlist.show();

    connect(&m_allchatlist, &ChatListWidget::onItemClicked, this, &MainWindow::onView);

    QList<Message> messages1 =
    {
        Message("明月几时有？把酒问青天。不知天上宫阙，今夕是何年", "3/24/23", MsgType::Sender),
        Message("我欲乘风归去，又恐琼楼玉宇，高处不胜寒。起舞弄清影，何似在人间。", "3/24/23", MsgType::Receive),
        Message("转朱阁，低绮户，照无眠。不应有恨，何事长向别时圆？", "3/24/23", MsgType::Sender),
        Message("人有悲欢离合，月有阴晴圆缺，此事古难全。但愿人长久，千里共婵娟。", "3/24/23", MsgType::Receive)
    };
    QList<Message> messages2 =
    {
        Message("寒蝉凄切，对长亭晚，骤雨初歇。  都门帐饮无绪，留恋处，兰舟催发。  执手相看泪眼，竟无语凝噎。", "3/24/23", MsgType::Sender),
        Message("念去去，千里烟波，暮霭沉沉楚天阔。  多情自古伤离别，更那堪，冷落清秋节！", "3/24/23", MsgType::Receive),
        Message("今宵酒醒何处？杨柳岸，晓风残月。  此去经年，应是良辰好景虚设。  便纵有千种风情，更与何人说？", "3/24/23", MsgType::Sender)
    };
    QList<Message> messages3 =
    {
        Message("千呼万唤始出来，犹抱琵琶半遮面。转轴拨弦三两声，未成曲调先有情。", "3/24/23", MsgType::Sender),
        Message("大弦嘈嘈如急雨，小弦切切如私语。嘈嘈切切错杂弹，大珠小珠落玉盘。", "3/24/23", MsgType::Receive),
        Message("十三学得琵琶成，名属教坊第一部。曲罢曾教善才服，妆成每被秋娘妒。", "3/24/23", MsgType::Sender),
        Message("同是天涯沦落人，相逢何必曾相识！今夜闻君琵琶语，如听仙乐耳暂明。", "3/24/23", MsgType::Receive),
    };

    m_allchatlist.AddItem(new Friend("wxid_1", "张三", ":/images/man.svg", true, "3/24/23", 4, messages1));
    m_allchatlist.AddItem(new Friend("wxid_2", "李四", ":/images/woman.svg", false, "3/24/23", 3, messages2));
    m_allchatlist.AddItem(new Friend("wxid_3", "王五", ":/images/man.svg", true, "3/24/23", 4, messages3));

    m_allchatlist.AddItem(new Friend("wxid_1", "马六", ":/images/man.svg", true, "3/24/23", 3, messages1));
    m_allchatlist.AddItem(new Friend("wxid_2", "吴八", ":/images/woman.svg", false, "3/24/23", 4, messages2));
    m_allchatlist.AddItem(new Friend("wxid_3", "孙九", ":/images/man.svg", true, "3/24/23", 3, messages3));
}

Area MainWindow::getArea(int x, int y)
{
    auto rect = geometry();
    if (x <= 10)
    {
        if (y >= rect.height() - 10)
            return Area::BottomLeft;
        else if (y <= 10)
            return  Area::TopLeft;
        else
            return  Area::Left;
    }
    else if (x >= rect.width() - 10)
    {
        if (y >= rect.height() - 10)
            return Area::BottomRight;
        else if (y <= 10)
            return Area::TopRight;
        else
            return Area::Right;
    }
    else
    {
        if (y >= rect.height() - 10)
            return  Area::Bottom;
        else if (y <= 10)
            return Area::Top;
    }
    return Area::Center;
}

void MainWindow::stickEvent()
{
    ui->stickBtn->setIcon(QIcon(QPixmap(ui->stickBtn->isChecked() ? ":/images/sticky2.svg" : ":/images/sticky.svg")));
}

void MainWindow::mininEvent()
{
    setWindowState(Qt::WindowState::WindowMinimized);
}

void MainWindow::clozeEvent()
{
    close();
}

void MainWindow::maxinEvent()
{
    switch (windowState())
    {
    case Qt::WindowState::WindowMaximized:
        setWindowState(Qt::WindowState::WindowNoState);
        ui->maxinBtn->setIcon(QIcon(QPixmap(":/images/max.svg")));
        break;
    case Qt::WindowState::WindowNoState:
        setWindowState(Qt::WindowState::WindowMaximized);
        ui->maxinBtn->setIcon(QIcon(QPixmap(":/images/max3.svg")));
        break;
    }
}

void MainWindow::selectEvent()
{
    m_btn->setChecked(false);
    m_btn = (QPushButton*)sender();
    m_btn->setChecked(true);

    switch (m_page) {
    case AllChat:
        ui->gridLayout_2->removeWidget(&m_allchatlist);
        m_allchatlist.hide();
        break;
    case Contact:
        ui->gridLayout_2->removeWidget(&m_contactlist);
        m_contactlist.hide();
        break;
    case Collect:
        break;
    }

    auto name = m_btn->objectName();
    if (name == "allchatBtn") {
        m_page = ChatPage::AllChat;
        ui->gridLayout_2->addWidget(&m_allchatlist);
        m_allchatlist.show();
    } else if (name == "contactBtn") {
        m_page = ChatPage::Contact;
        ui->gridLayout_2->addWidget(&m_contactlist);
        m_contactlist.show();
    } else if (name == "collectBtn") {
        m_page = ChatPage::Collect;
    }
}

void MainWindow::onView(QVariant var, PageType type)
{
    if (var.canConvert<Friend*>() && type == PageType::AllChatView) {
        auto data = var.value<Friend*>();
        ui->gridLayout_4->addWidget(&m_allchatWidget);
        m_allchatWidget.setData(data);
        m_allchatWidget.show();
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        isMouseDown = true;
        m_pressRect = geometry();
        m_pressPos = event->globalPos();
        m_posx = mapFromGlobal(m_pressPos).x();
        m_posy = mapFromGlobal(m_pressPos).y();
        m_area = getArea(event->x(), event->y());
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if (isMouseDown)
    {
        auto currentPos = QCursor::pos();
        auto moveSize = currentPos - m_pressPos;
        auto currentRect = m_pressRect;
        switch (m_area)
        {
        case Top:
            currentRect.setTop(m_pressRect.top() + moveSize.y());
            break;
        case Bottom:
            currentRect.setBottom(m_pressRect.bottom() + moveSize.y());
            break;
        case Left:
            currentRect.setLeft(m_pressRect.left() + moveSize.x());
            break;
        case Right:
            currentRect.setRight(m_pressRect.right() + moveSize.x());
            break;
        case TopLeft:
            currentRect.setTopLeft(m_pressRect.topLeft() + moveSize);
            break;
        case BottomRight:
            currentRect.setBottomRight(m_pressRect.bottomRight() + moveSize);
            break;
        case TopRight:
            currentRect.setTopRight(m_pressRect.topRight() + moveSize);
            break;
        case BottomLeft:
            currentRect.setBottomLeft(m_pressRect.bottomLeft() + moveSize);
            break;
        case Center:
            move(currentPos.x() - m_posx, currentPos.y() - m_posy);
            break;
        }
        if (m_area != Center && currentRect.width() >= 700 && currentRect.height() >= 500)
        {
            setGeometry(currentRect);
        }
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    isMouseDown = false;
    QApplication::restoreOverrideCursor();
}

void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    switch (windowState())
    {
    case Qt::WindowMaximized:
        setWindowState(Qt::WindowNoState);
        break;
    case Qt::WindowNoState:
        setWindowState(Qt::WindowMaximized);
        break;
    }
}

bool MainWindow::eventFilter(QObject *sender, QEvent *event)
{
    if (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverMove || event->type() == QEvent::HoverLeave)
    {
        auto pos = mapFromGlobal(QCursor::pos());
        auto area = getArea(pos.rx(), pos.ry());
        switch (area)
        {
        case Top:
        case Bottom:
            setCursor(Qt::SizeVerCursor);
            break;
        case Left:
        case Right:
            setCursor(Qt::SizeHorCursor);
            break;
        case TopLeft:
        case BottomRight:
            setCursor(Qt::SizeFDiagCursor);
            break;
        case TopRight:
        case BottomLeft:
            setCursor(Qt::SizeBDiagCursor);
            break;
        case Center:
        default:
            setCursor(Qt::ArrowCursor);
            break;
        }
    }
    return QMainWindow::eventFilter(sender, event);
}
